﻿@model Cxuu.Models.EditArticleCate
@{
    ViewData["Title"] = "保存";
}

<div class="content-top-nav">
    <div class="layui-row">
        <div class="layui-col-md11">
            <h3 style="color:#444">
                <i class="layui-icon layui-icon-home"></i>
               栏目编辑
            </h3>
        </div>
        <div class="layui-col-md1" style="text-align:right;">
            <button class="layui-btn layui-btn-sm" onclick="loadMain('/Admin/Category/')">返回列表</button>
        </div>
    </div>
    <hr>
</div>

<div class="layui-fluid" style="margin-bottom:50px;">
    <div class="layui-form">
        <form class="layui-form" action="" lay-filter="FormEdit" enctype="multipart/form-data">
            <div class="layui-form-item">
                <label class="layui-form-label required">上级栏目</label>
                <div class="layui-input-inline">
                    <select name="Pid" id="Pid" lay-verify="">
                        <option value="0">顶级</option>
                        @foreach (var item in ViewBag.articleCateData)
                        {
                            @if (item.Id == ViewBag.id)
                            {
                                <option value="@item.Id" Disabled="disabled">@item.Title</option>
                            }
                            else if (item.Pid == ViewBag.id)
                            {
                                <option value="@item.Id" selected="selected">@item.Title</option>
                            }
                            else
                            {
                                <option value="@item.Id">@item.Title</option>
                            }
                        }
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">不能选自己作为上级</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">栏目名称</label>
                <div class="layui-input-inline">
                    <input type="text" asp-for="Name" lay-verify="required" lay-reqtext="栏目名称不能为空" placeholder="请输入栏目名称" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">栏目类型</label>
                <div class="layui-input-inline">
                    <input asp-for="Type" type="radio" value="true" title="频道" checked>
                    <input asp-for="Type" type="radio" value="false" title="列表">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">栏目模板</label>
                <div class="layui-input-inline">
                    <input type="text" asp-for="Theme" placeholder="请输入栏目模板" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">栏目排序</label>
                <div class="layui-input-inline">
                    <input type="text" asp-for="Sort" value="" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">只能填写数字，如： 2</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">前台显条数</label>
                <div class="layui-input-inline">
                    <input type="text" asp-for="Num" value="" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">只能填写数字，如： 15</div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="saveBtn" id="saveBtn">@ViewData["Title"]</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery;

        form.render();

        var editId = @ViewBag.id+1;
        //console.log(editId);
        if (editId > 1) {
            $.getJSON("/Admin/Category/AddOrEdit?id=@ViewBag.id&edit=1", function (res) {
                if (res.status == 1) {
                    form.val('FormEdit', {
                        Id: res.data.id,
                        Pid: res.data.pid,
                        Name: res.data.name,
                        Type: res.data.type,
                        Theme: res.data.theme,
                        Num: res.data.num,
                        Sort: res.data.sort,
                    });
                    if (res.data.type)
                        $('input[name="Type"][value="true"]').prop('checked', 'checked');
                    else
                        $('input[name="Type"][value="false"]').prop('checked', 'checked');
                    form.render('radio');
                }
                //console.log("json", res+"abc");
            })
        } else {
            form.val('FormEdit', {
                Pid: @ViewBag.pid,
            });
            $('input[name="Type"][value="true"]').prop('checked', 'checked');
            form.render('radio');
         }

        var dataPost = function (url,data) {
            $.post(url, data, function (res) {
                if (res.status == 1) {
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        loadMain('/Admin/Category/Index/' + res.cid);
                    });
                } else {
                    layer.msg(res.msg, { icon: 2, anim: 6, time: 5000 });
                }
            });
        }

        //监听提交
        //监听提交
        $('#saveBtn').click(function () {
       // form.on('submit(saveBtn)', function (data) {   form.val('FormEdit')
            //var data = {
            //    Pid: data.field.Pid,
            //    Name: data.field.Name,
            //    Type: data.field.Type,
            //    Theme: data.field.Theme,
            //    Num: data.field.Num,
            //    Sort: data.field.Sort
            //};
            if (editId == 1)
                dataPost('/Admin/Category/CreatePost', form.val('FormEdit'));
             else
                dataPost('/Admin/Category/EditPost?id=@ViewBag.id', form.val('FormEdit'));
            return false;
        });


    });

</script>